package com.example.damaimaven.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.example.damaimaven.entity.Product;
import com.example.damaimaven.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zzh
 * @since 2024-07-09
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    ProductMapper productMapper;

    @RequestMapping("querIndexProduct")
    public Map<String, Object> queryIndexProduct(){
        //分页
        Page<Product> page = new PageDTO<>(1,10);
        //排序
        page.addOrder(OrderItem.desc("pid"));
        //查询最新的10个商品
        Page<Product> news = productMapper.selectPage(page,null);
        //重新构建新的分页和排序
        page = new PageDTO<>(1,10);
        page.addOrder(OrderItem.asc("pid"));
        //构建条件对象
        LambdaQueryWrapper<Product> lwq = new LambdaQueryWrapper<Product>();
        // is_hot = 1 热门商品条件
        lwq.eq(Product::getIsHot,1);
        //查询热门的10个商品
        Page<Product> hots = productMapper.selectPage(page,null);

        Map<String, Object> ret = new HashMap<>();
        ret.put("news", news);
        ret.put("hots", hots);
        return ret;

    }

    @RequestMapping("findById")
    public Product findById(int pid) {
        return productMapper.selectById(pid);
    }


    @RequestMapping("queryByCsid")
    public Page<Product> queryByCsid(int csid,int size, int page) {
        LambdaQueryWrapper<Product> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Product::getCsid, csid);
        Page<Product> pageObj= new PageDTO<>(page,size);
        Page<Product> productPage = productMapper.selectPage(pageObj, lqw);
        return productPage;
    }

    @RequestMapping("queryByCid")
    public Page<Product> queryByCid(int cid,int size, int page) {
        LambdaQueryWrapper<Product> lqw = new LambdaQueryWrapper<>();
        lqw.inSql(Product::getCsid,"select csid from categorysecond where cid="+cid);
        Page<Product> pageObj= new PageDTO<>(page,size);
        Page<Product> productPage = productMapper.selectPage(pageObj, lqw);
        return productPage;
    }

}
